package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import DataBase.DAO;
//import Modelo.BDlocal;
import Modelo.Rol;
import Modelo.Usuario;

public class Ventanalogin extends JFrame {

	private JPanel contentPane;
	private JTextField txtFieldId;
	private JLabel lblPass;
	private JTextField txtFieldPass;
	private Principal ventana;
	private JLabel lblUsuario;
	private Ventanalogin yo = this;
	private ArrayList<Usuario> Users;
	//private BDlocal BaseDatos;
	
	///////////BD////////
	private DAO op;
	private Connection con;///

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Ventanalogin frame = new Ventanalogin();
					frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}

			}




		});
	}

	/**
	 * Create the frame.
	 */
	public Ventanalogin() {
		this.inicialize();
		
		

	}

	private void inicialize() {
		
		String sfichero="derby.log";
		boolean existe=true;
		File fichero=new File(sfichero);
		if(fichero.exists())existe=true;
		else existe=false;
		
		/////bd///
		op=new DAO();
		op.conectar();//solo una vez
		con=op.getBD(); //solo aqui luego se va pasando
		//////////////
		if(!existe){
			op.insert("tienda", "id_tienda", "001", con);
			op.insert("empleado", "dni,pass,nombre,rol,direccion,telefono,fechainicio,tienda", "666,1231,'david','admin','orense',12345678,'1999-10-11',001", con);
		}
	
		/////////bd///////////////
	//	op.insert("tienda", "id_tienda", "001", con);
		//op.insert("empleado", "dni,pass,nombre,rol,direccion,telefono,fechainicio,tienda", "48228535,1231,'david','admin','orense',12345678,'1999-10-11',001", con);
		
		////////////////////////////////////
		setType(Type.UTILITY);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 317, 188);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		lblUsuario = new JLabel("ID USER");
		lblUsuario.setBounds(50, 27, 78, 20);
		panel.add(lblUsuario);

		txtFieldId = new JTextField();
		txtFieldId.setBounds(149, 27, 86, 20);
		panel.add(txtFieldId);
		txtFieldId.setColumns(10);

		/**lblPass = new JLabel("Contrase�a");
		lblPass.setBounds(50, 58, 78, 17);
		panel.add(lblPass);

		txtFieldPass = new JTextField();
		txtFieldPass.setBounds(149, 58, 86, 20);
		panel.add(txtFieldPass);
		txtFieldPass.setColumns(10);**/

		JButton btnNewButton = new JButton("LOGIN");
		btnNewButton.setBounds(35, 95, 89, 23);
		btnNewButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				String rol=op.getID(txtFieldId.getText(), con);
				txtFieldId.setText("");
				if (rol != null) {
					switch (rol) {
					case "admin":
						ventana = new Principal(con,yo);
						ventana.btnInformes.setEnabled(false);
						//yo.dispose();
						yo.setVisible(false);
						break;

					case "Jefealmacen":
						ventana = new Principal(con,yo);
						ventana.btnGestion.setEnabled(false);
						ventana.btnVentas.setEnabled(false);
						yo.dispose();
						break;
					case "JefeVentas":
						ventana = new Principal(con,yo);
						ventana.btnGestion.setEnabled(false);
						ventana.btnAlmacen.setEnabled(false);
						yo.dispose();
						break;
					default:
						break;

					}
				}else{
					int ventana=JOptionPane.showConfirmDialog(null,"ID NO ENCONTRADO", "G-Market", JOptionPane.CLOSED_OPTION);
					txtFieldId.setText("");
				}
				
			}
		});
		
		panel.add(btnNewButton);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(149, 95, 89, 23);
		btnCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				int respuesta = JOptionPane.showConfirmDialog(null,
						"Deseas Salir", "G-Market", JOptionPane.YES_NO_OPTION);

				if (respuesta == JOptionPane.YES_OPTION)
					System.exit(0);
				}
				
			});
		panel.add(btnCancel);
	}



	
	
}
